import joblib
from xbase_util.common_util import check_path

from src.bean.cache_value_enum import CacheValueEnum
from src.constant import update_local_cache


class CacheValueUtil:
    @classmethod
    def get_cache_value(cls, data_cache, enum_name, is_abnormal):
        # 通过检查name是否是枚举值
        if enum_name in list(CacheValueEnum):
            col = enum_name.value
            # data_cache = config.get_config('data_cache')
            if col in data_cache['abnormal' if is_abnormal is True else 'normal']:
                return data_cache['abnormal' if is_abnormal is True else 'normal'][col]
            else:
                return 0, 0, 0
        else:
            raise ValueError(f"获取值错误: {enum_name.value}")

    @classmethod
    def update_cache_value(cls, enum_name, data_cache, value, is_abnormal):
        if enum_name in list(CacheValueEnum):
            col = enum_name.value
            data_cache['abnormal' if is_abnormal is True else 'normal'][col] = value
            return data_cache
        else:
            raise ValueError(f"保存值错误: {enum_name}")

    @classmethod
    def save_data_cache(cls, data_cache, data_path):
        if update_local_cache:
            joblib.dump(data_cache, check_path(data_path))
